package com.site.blog.my.core.controller.blog;

import com.site.blog.my.core.controller.vo.BlogDetailVO;
import com.site.blog.my.core.entity.BlogLink;
import com.site.blog.my.core.entity.BlogTag;
import com.site.blog.my.core.service.BlogService;
import com.site.blog.my.core.service.ConfigService;
import com.site.blog.my.core.service.LinkService;
import com.site.blog.my.core.service.TagService;
import com.site.blog.my.core.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author yz
 * @ClassNameMyBolgController
 * @create 2021-07-05 16:24
 * @Version 1.0
 */
@Controller
public class MyBlogController {
    public static String theme = "amaze";
    @Autowired
    private ConfigService configService;

    @Autowired
    private TagService tagService;

    @Autowired
    private LinkService linkService;

    @Autowired
    private BlogService blogService;

    @GetMapping({"/", "/index", "index.html"})
    public String index(HttpServletRequest request) {
        return this.page(request, 1);
    }

    /**
     * 首页 分页数据
     *
     * @return
     */
    @GetMapping({"/page/{pageNum}"})
    public String page(HttpServletRequest request, @PathVariable("pageNum") int pageNum) {
        PageResult blogPageResult = blogService.getBlogsForIndexPage(pageNum);
        if (blogPageResult == null) {
            return "error/error_404";
        }
        request.setAttribute("blogPageResult", blogPageResult);
        request.setAttribute("newBlogs", blogService.getBlogListForIndexPage(1));
        request.setAttribute("hotBlogs", blogService.getBlogListForIndexPage(0));
        request.setAttribute("hotTags", tagService.getBlogTagCountForIndex());
        request.setAttribute("pageName", "首页");
        request.setAttribute("configurations", configService.getAllConfigs());
        return "blog/" + theme + "/index";
    }
    /*@GetMapping({"/common"})
    public String common(HttpServletRequest request){
        request.setAttribute("configurations",configService.getAllConfigs());

        return "blog/" + theme + "/index";
    }*/
   /* @GetMapping({"/tag/{tagName}"})
    public String tag(HttpServletRequest request, @PathVariable("tagName") String tagName){

        return "blog/" + theme + "/index";
    }*/
    @GetMapping({"/link"})
    public String link(HttpServletRequest request){
        request.setAttribute("pageName","友情链接");
        Map<Byte, List<BlogLink>> linksMap = linkService.getLinksForLinkPage();
        if (linksMap != null) {
            //判断友链类别并封装数据 0-友链 1-推荐 2-个人网站
        if (linksMap.containsKey((byte)0)){
                request.setAttribute("favoriteLinks",linksMap.get((byte) 0));
            }
            if (linksMap.containsKey((byte) 1)) {
                request.setAttribute("recommendLinks", linksMap.get((byte) 1));
            }
            if (linksMap.containsKey((byte) 2)) {
                request.setAttribute("personalLinks", linksMap.get((byte) 2));
            }
        }
        request.setAttribute("configurations", configService.getAllConfigs());
        return "blog/" + theme + "/link";
    }
    //详情页
    @GetMapping({"/blog/{blogId}", "/article/{blogId}"})
    public String detail(HttpServletRequest request, @PathVariable("blogId") Long blogId, @RequestParam(value = "commentPage", required = false, defaultValue = "1") Integer commentPage) {
        BlogDetailVO blogDetailVO = blogService.getBlogDetail(blogId);
        if (blogDetailVO != null) {
            request.setAttribute("blogDetailVO", blogDetailVO);
            //request.setAttribute("commentPageResult", commentService.getCommentPageByBlogIdAndPageNum(blogId, commentPage));
        }
        request.setAttribute("pageName", "详情");
        request.setAttribute("configurations", configService.getAllConfigs());
        return "blog/" + theme + "/detail";
    }
}
